/*     */ package com.newcrm.webapi;
/*     */ 
/*     */ import com.newcrm.util.MD5;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import javax.servlet.ServletConfig;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class UploadFile extends HttpServlet
/*     */ {
/*     */   private static final long serialVersionUID = 174079866443481752L;
/*  29 */   private static Logger logger = Logger.getLogger(UploadFile.class.getName());
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*  43 */     doGet(request, response);
/*     */   }
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*  61 */     response.setContentType("text/html");
/*  62 */     PrintWriter out = response.getWriter();
/*  63 */     DiskFileItemFactory fac = new DiskFileItemFactory();
/*  64 */     ServletFileUpload upload = new ServletFileUpload(fac);
/*  65 */     upload.setHeaderEncoding("utf-8");
/*  66 */     List fileList = null;
/*     */     try {
/*  68 */       fileList = upload.parseRequest(request);
/*     */     } catch (FileUploadException e) {
/*  70 */       logger.warn("upload parse request failed", e);
/*     */     }
/*     */ 
/*  73 */     if (fileList == null) {
/*  74 */       out.print("");
/*  75 */       out.flush();
/*  76 */       out.close();
/*  77 */       return;
/*     */     }
/*     */ 
/*  80 */     Iterator it = fileList.iterator();
/*  81 */     while (it.hasNext()) {
/*  82 */       FileItem item = (FileItem)it.next();
/*  83 */       if (!item.isFormField())
/*     */       {
/*  87 */         String fileName = item.getName();
/*  88 */         if ((fileName != null) && (!fileName.trim().equals("")))
/*     */         {
/*  93 */           String extName = "";
/*  94 */           if (fileName.lastIndexOf(".") >= 0) {
/*  95 */             extName = fileName.substring(fileName.lastIndexOf("."))
/*  96 */               .toLowerCase();
/*     */           }
/*     */ 
/* 100 */           if ((extName.contains(".exe")) || (extName.contains(".jsp"))) {
/* 101 */             out.print("");
/* 102 */             out.flush();
/* 103 */             out.close();
/* 104 */             return;
/*     */           }
/*     */ 
/* 107 */           String filePath = 
/* 108 */             MD5.getMD5(String.valueOf(System.currentTimeMillis())) + extName;
/* 109 */           String path = getServletConfig().getServletContext()
/* 110 */             .getRealPath("uploadfiles/");
/* 111 */           File file = new File(path, filePath);
/*     */           try {
/* 113 */             item.write(file);
/* 114 */             out.print(filePath);
/*     */           } catch (Exception e) {
/* 116 */             out.println("");
/* 117 */             logger.warn("upload file failed", e);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 120 */     out.flush();
/* 121 */     out.close();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.UploadFile
 * JD-Core Version:    0.6.1
 */